<?php
/**
 * param
 * Created by PhpStorm.
 * User: UI
 * Date: 2016/7/6
 * Time: 14:50
 */

class Param
{
    //route config
    private $routeConfig = '';

    public function __construct(){
        if(!get_magic_quotes_gpc()){
            $_POST = new_addslashes($_POST);//global.function.class.php
            $_GET = new_addslashes($_GET);
            $_REQUEST = new_addslashes($_REQUEST);
            $_COOKIE = new_addslashes($_COOKIE);
        }

        $this->routeConfig = AcmsBase::loadConfig('route',SITE_URL)?AcmsBase::loadConfig('route',SITE_RUL):AcmsBase::loadConfig('route','default');
//        var_dump($this->routeConfig);die;

        if(isset($this->routeConfig['data']['POST']) && is_array($this->routeConfig['data']['POST'])){
                foreach($this->routeConfig['data']['POST'] as $_key => $_value){
                    if(isset($_POST[$_key])){
                        $_POST[$_key] = $_value;
                    }
                }
        }

        if(isset($this->routeConfig['data']['GET']) && is_array($this->routeConfig['data']['GET'])){
            foreach($this->routeConfig['data']['GET'] as $_key => $_value){
                if(isset($_GET[$_key])){
                    $_GET[$_key] = $_value;
                }
            }
        }

        if(isset($_GET['page'])){
            $_GET['page'] = max(intval($_GET['page']),1);
            $_GET['page'] = min(intval($_GET['page']),10000000);
        }
        return true;
    }

    /**
     * getModule
     */
    public function route_m(){
        $m = isset($_GET['m']) && !empty($_GET['m']) ? $_GET['m'] : (isset($_POST['m']) && !empty($_POST['m']) ? $_POST['m'] : '');
        $m = $this->safe_deal($m);
        if(empty($m)){
            return $this->routeConfig['m'];
        }else{
            if(is_string($m)){
                return $m;
            }
        }
    }

    public function route_c(){
        $c = isset($_GET['c'])&&!empty($_GET['c'])?$_GET['c']:(isset($_POST['c'])&&!empty($_POST['c'])?$_POST['c']:'');
        $c = $this->safe_deal($c);
        if(empty($c)){
            return $this->routeConfig['c'];
        }else{
            if(is_string($c)){
                return $c;
            }
        }
    }

    public function route_a(){
        $a = isset($_GET['a'])&&!empty($_GET['a'])?$_GET['a']:(isset($_POST['a'])&&!empty($_POST['a'])?$_POST['a']:'');
        $a = $this->safe_deal($a);
        if(empty($a)){
            return $this->routeConfig['a'];
        }else{
            if(is_string($a)){
                return $a;
            }
        }
    }

    /**
     * safeDeal
     * @param $str
     * @return mixed
     */
    private function safe_deal($str){
        return str_replace(['/','.'],'',$str);

    }

}